package com.app.biz.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.app.common.annotation.Anonymous;
import com.app.common.config.AppConfig;
import com.app.common.utils.file.FileUploadUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.app.common.annotation.Log;
import com.app.common.core.controller.BaseController;
import com.app.common.core.domain.AjaxResult;
import com.app.common.enums.BusinessType;
import com.app.biz.domain.BizIndustrialSectorMetrics;
import com.app.biz.service.IBizIndustrialSectorMetricsService;
import com.app.common.utils.poi.ExcelUtil;
import com.app.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 分行业企业主要经济指标临时Controller
 *
 * @author SangGuangCan
 * @date 2024-01-02
 */
@RestController
@RequestMapping("/biz/metrics")
public class BizIndustrialSectorMetricsController extends BaseController
{
    private final IBizIndustrialSectorMetricsService bizIndustrialSectorMetricsService;

    @Autowired
    public BizIndustrialSectorMetricsController(IBizIndustrialSectorMetricsService bizIndustrialSectorMetricsService){
        this.bizIndustrialSectorMetricsService = bizIndustrialSectorMetricsService;
    }

    /**
     * 查询分行业企业主要经济指标临时列表
     */
    @PreAuthorize("@ss.hasPermi('biz:metrics:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizIndustrialSectorMetrics bizIndustrialSectorMetrics)
    {
        startPage();
        List<BizIndustrialSectorMetrics> list = bizIndustrialSectorMetricsService.selectBizIndustrialSectorMetricsList(bizIndustrialSectorMetrics);
        return getDataTable(list);
    }

    /**
     * 导出分行业企业主要经济指标临时列表
     */
    @PreAuthorize("@ss.hasPermi('biz:metrics:export')")
    @Log(title = "分行业企业主要经济指标临时", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizIndustrialSectorMetrics bizIndustrialSectorMetrics)
    {
        List<BizIndustrialSectorMetrics> list = bizIndustrialSectorMetricsService.selectBizIndustrialSectorMetricsList(bizIndustrialSectorMetrics);
        ExcelUtil<BizIndustrialSectorMetrics> util = new ExcelUtil<BizIndustrialSectorMetrics>(BizIndustrialSectorMetrics.class);
        util.exportExcel(response, list, "分行业企业主要经济指标临时数据");
    }

    /**
     * 获取分行业企业主要经济指标临时详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:metrics:query')")
    @GetMapping(value = "/{baseId}")
    public AjaxResult getInfo(@PathVariable("baseId") String baseId)
    {
        return success(bizIndustrialSectorMetricsService.selectBizIndustrialSectorMetricsByBaseId(baseId));
    }

    /**
     * 新增分行业企业主要经济指标临时
     */
    @PreAuthorize("@ss.hasPermi('biz:metrics:add')")
    @Log(title = "分行业企业主要经济指标临时", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizIndustrialSectorMetrics bizIndustrialSectorMetrics)
    {
        return toAjax(bizIndustrialSectorMetricsService.insertBizIndustrialSectorMetrics(bizIndustrialSectorMetrics));
    }

    /**
     * 修改分行业企业主要经济指标临时
     */
    @PreAuthorize("@ss.hasPermi('biz:metrics:edit')")
    @Log(title = "分行业企业主要经济指标临时", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizIndustrialSectorMetrics bizIndustrialSectorMetrics)
    {
        return toAjax(bizIndustrialSectorMetricsService.updateBizIndustrialSectorMetrics(bizIndustrialSectorMetrics));
    }

    /**
     * 删除分行业企业主要经济指标临时
     */
    @PreAuthorize("@ss.hasPermi('biz:metrics:remove')")
    @Log(title = "分行业企业主要经济指标临时", businessType = BusinessType.DELETE)
    @DeleteMapping("/{baseIds}")
    public AjaxResult remove(@PathVariable String[] baseIds)
    {
        return toAjax(bizIndustrialSectorMetricsService.deleteBizIndustrialSectorMetricsByBaseIds(baseIds));
    }

    /**
     * <p>
     *     将临时表中的数据迁移到正式表虽
     * </p>
     * @return
     */
    @Anonymous
    @PostMapping("/transferDataInOneClick")
    public AjaxResult transferDataInOneClick(){

        return toAjax(bizIndustrialSectorMetricsService.transferDataInOneClick());
    }

    /**
     * <p>
     *     清空所有的分行业企业主要经济指标临时
     * </p>
     * @return
     */
    @DeleteMapping("/clearAllTableData")
    public AjaxResult clearAllTableData(){
        bizIndustrialSectorMetricsService.clearAllTableData();
        return AjaxResult.success();
    }

    /**
     * <p>
     *     导入分行业企业主要经济指标数据
     * </p>
     * @param params 前端传来的参数，目前只有一个月份
     * @param multipartFile 上传的Excel文件
     * @return 导入是否成功
     */
    @PostMapping("/importExcel")
    public AjaxResult importExcel(@RequestParam Map<String,String> params, @RequestPart("file") MultipartFile multipartFile){
        try{
            String yearMonth = params.get("yearMonth");
            if(StringUtils.isBlank(yearMonth)){
                return AjaxResult.warn("请选择导入的年月");
            }
            List<BizIndustrialSectorMetrics> list =  bizIndustrialSectorMetricsService.selectBizIndustrialSectorMetricsListNoParams();
            if(list != null && !list.isEmpty()){
                return AjaxResult.warn("导入前请先清空数据");
            }
            /* 上传文件的路径 */
            String uploadPath = FileUploadUtils.upload(AppConfig.getProfile(),multipartFile);
            String absoluteExcelPath = FileUploadUtils.replaceAbsolutePath(uploadPath);
            return bizIndustrialSectorMetricsService.importExcel(absoluteExcelPath,yearMonth);
        }catch (IOException e){
            return AjaxResult.error("导入失败");
        }
    }
}
